package com.ssy.lingxi.common.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 对象序列号工具类
 * @author wyx
 * @version 2.0.0
 * @date 2022-03-10
 **/
public class ObjectSerializableUtil {
    /**
     * Json序列化对象
     * @param obj 对象
     * @return json字符串
     */
    public static String serialize(Object obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(obj);
        } catch (Exception ignored) {
            return "";
        }
    }

    /**
     * Json反序列化对象
     * @param json   Json字符串
     * @param tClass 反序列化对象的Class
     * @return 对象
     */
    public static <T> T deserialize(String json, Class<T> tClass) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return mapper.readValue(json, tClass);
        } catch (Exception ignored) {
            return null;
        }
    }
}
